/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import com.aptech.bluepumpkin.Event;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang3.StringEscapeUtils;

/**
 *
 * @author Trong Nguyen
 */
public class Uility implements Serializable {

   public static String decodeString(String text){
       if(null==text){
           return "";
       }
      
       String str = StringEscapeUtils.unescapeHtml4(text);
        System.out.println("Escape :"+str);
    return   RemoveHtmlTags.removeHTML(str);
   }
    public static Date maxDOB() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -18);
        return cal.getTime();
    }

    public static Date minDOB() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -60);
        return cal.getTime();
    }

    public static void redirectPage(FacesContext context, Event event, String destinationPage) {

       
            try {
                context.getExternalContext().redirect(destinationPage);
            } catch (Exception ex) {
                Logger.getLogger(Uility.class.getName()).log(Level.SEVERE, null, ex);
            }
        
    }

    public static String createRandomPassword(int PasswordLength) {

        String _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
        Random randNum = new Random();
        char[] chars = new char[PasswordLength];
        int allowedCharCount = _allowedChars.length();
        for (int i = 0; i < PasswordLength; i++) {
            chars[i] = _allowedChars.charAt((int) ((allowedCharCount) * randNum.nextDouble()));
        }
        return new String(chars);

    }

    public static void sendMail(String to,String subject, String content) throws MessagingException {
        String host = "smtp.gmail.com";
        String from = "blue.pumpkin.group3.aptech@gmail.com";
        String pass = "trong1984";
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true"); // added this line
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        // String[] to = {"to@gmail.com"}; // added this line
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        InternetAddress toAddress = new InternetAddress(to);
        // To get the array of addresses
        message.addRecipient(Message.RecipientType.TO, toAddress);

        message.setSubject(subject);
        message.setText(content);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        System.out.println("----Email is sent.");

    }

    /**
     * Return MD5 String of input text
     *
     * @param text
     * @return
     */
    public static String getMD5(String text) {
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(text.getBytes(), 0, text.length());
            return new BigInteger(1, m.digest()).toString(16);

        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Uility.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * Compare input String source with md5 String. It's used for comparing
     * input password with password in which stored in Database
     *
     * @param source
     * @param md5Destination
     * @return
     */
    public static boolean compareMD5(String source, String md5Destination) {

        String md2 = getMD5(source);
        return md2.equals(md5Destination);
    }
}
